﻿package mortal.game.utils
{
    import flash.events.*;
    import flash.net.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;

    public class StoreUpUtil extends Object
    {
        private static var file:FileReference;
        private static var onSaveComplete:Function;

        public function StoreUpUtil()
        {
            return;
        }// end function

        public static function downloadToDesktop(param1:Function) : void
        {
            var name:String;
            var onComplete:* = param1;
            if (file)
            {
                return;
            }
            onSaveComplete = onComplete;
            var url:String;
            url = url + ("URL=" + ParamsConst.instance.favoritesUrl + "\r\n");
            url = url + "IconIndex=0\r\n";
            url = url + "IconFile=http://fbdl-cdn.xunwan.com/favicon.ico\r\n";
            file = new FileReference();
            file.addEventListener(Event.COMPLETE, cancelEvents);
            file.addEventListener(IOErrorEvent.IO_ERROR, cancelEvents);
            file.addEventListener(Event.CANCEL, cancelEvents);
            file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, cancelEvents);
            try
            {
                name = ParamsConst.instance.gameName;
                name = name + (NameUtil.getProxyName(Cache.instance.role.entityInfo.entityId, true) + Cache.instance.role.entityInfo.name);
                file.save(url, name + ".url");
            }
            catch (e:Error)
            {
                cancelEvents(null);
            }
            return;
        }// end function

        private static function cancelEvents(event:Event) : void
        {
            if (file)
            {
                file.cancel();
                file.removeEventListener(Event.COMPLETE, cancelEvents);
                file.removeEventListener(IOErrorEvent.IO_ERROR, cancelEvents);
                file.removeEventListener(Event.CANCEL, cancelEvents);
                file.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, cancelEvents);
                file = null;
                if (onSaveComplete != null)
                {
                    if (!event || event.type != Event.CANCEL)
                    {
                        onSaveComplete();
                    }
                    onSaveComplete = null;
                }
            }
            return;
        }// end function

    }
}
